﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json.Linq;
using FBEntities.FBObjects.User;
using FBEntities.FBConnections;
using FBEntities.FBConnections.User;

namespace FBEntities.FBObjects
{
    public class FBPost : IFBUpdatable
    {
        #region IFBObject Members
        public string type;
        public string id;
        public FBFrom from;
        public FBTo to;
        public string message;
        public string picture;
        public string link;
        public string name;
        public string icon;
        public string caption;
        public string source;
        public string description;
        public string attribution;
        public DateTime created_time;
        public DateTime updated_time;
        public int likes;
        public FBComments comments;
        public List<FBAction> actions;

        public FBPost()
        {
            actions = new List<FBAction>();
        }

        public FBCommentsConnection Comments
        {
            get
            {
                if (commentsConnection == null)
                    commentsConnection = new FBCommentsConnection();

                if (!commentsConnection.Parsed)
                    commentsConnection.Parse(id);

                return commentsConnection;
            }
            protected set
            {
                commentsConnection = value;
            }
        }
        public FBLikesConnection PeopleWhoLiked
        {
            get
            {
                if (peopleWhoLiked == null)
                    peopleWhoLiked = new FBLikesConnection();

                FBCache.BlockNextCache = true;
                if (!peopleWhoLiked.Parsed)
                    peopleWhoLiked.Parse(id);

                return peopleWhoLiked;
            }
            protected set
            {
                peopleWhoLiked = value;
            }
        }

        protected FBLikesConnection peopleWhoLiked;
        protected FBCommentsConnection commentsConnection;

        public void Parse(JObject text)
        {
            FBParseUtilities.Parse(text, this);
        }

        public bool Like(bool remove)
        {
            if (!FBParseUtilities.LikeREST(id, remove))
                return false;

            if (!updatePost())
                return false;

            if (Updated != null)
                Updated(this, new EventArgs());

            return true;
        }

        public FBComment AddComment(string comment)
        {
            var ret = FBParseUtilities.PostTextREST(id, "stream.addComment", new string[] { "comment", comment });

            updatePost();
            FBCache.BlockNextCache = true;
            commentsConnection.Parse(id);

            if (Updated != null)
                Updated(this, new EventArgs());

            if (commentsConnection.data == null || commentsConnection.data.Count == 0)
                return null;

            return commentsConnection.data.Last();
        }

        private bool updatePost()
        {
            string url = "https://graph.facebook.com/" + id + "?" + FBParseUtilities.authKey;

            FBCache.BlockNextCache = true;
            string json = FBCache.GetJSON(id, url, false);
            if (string.IsNullOrEmpty(json))
                return false;

            JObject o = JObject.Parse(json);
            actions = new List<FBAction>();
            peopleWhoLiked = new FBLikesConnection();
            commentsConnection = new FBCommentsConnection();

            Parse(o);
            return true;
        }

        #endregion

        public event UpdateEventHandler Updated;
    }

    public class FBProxyPerson : IFBObject
    {
        public string id;
        public string category;
        public string name;
        public FBPicture Picture;

        #region IFBObject Members

        public void Parse(JObject text)
        {
            FBParseUtilities.Parse(text, this);

            Picture = new FBPicture(id);
        }

        #endregion

        public FBPerson ToPerson()
        {
            FBPerson ret = new FBPerson();
            ret.id = id;

            return ret;
        }
    }

    public class FBFrom : FBProxyPerson { }
    public class FBTo : IFBObject
    {
        public List<FBProxyPerson> data;

        public FBTo()
        {
            data = new List<FBProxyPerson>();
        }

        #region IFBObject Members

        public void Parse(JObject text)
        {
            FBParseUtilities.Parse(text, this);
        }

        #endregion
    }

}
